import React, { useState, useEffect } from "react";
import "./index.css";
import {Button, Descriptions, Input} from "antd";
import {SHUTDOWN_LOAD_CALCULATION_FRONT_THREE} from "../../uitl/constant-filename";

const calculateResInit = {
  mainMaxLoad: null, // 主起落架最大停机载荷(kN)
  preMaxLoad: null, // 前起落架最大停机载荷(kN)
  preMinLoad: null, // 前起落架最小停机载荷(kN)
  preMaxBrakeLoad: null // 前起落架最大刹车的前轮载荷(kN)
}

/**
 * 停机载荷计算（前三点式）
 * @constructor
 */
function ShutdownLoadCalculationFrontThree() {

  // 飞机的最大总重(kg)
  const [weightMax, setWeightMax] = useState(null);
  // 前起落架与主起落架距离(m)
  const [space01, setSpace01] = useState(null);
  // 前起落架与前重心距离(m)
  const [space02, setSpace02] = useState(null);
  // 后重心与主起落架距离(m)
  const [space03, setSpace03] = useState(null);
  // 前后重心与地面距离(m)
  const [space04, setSpace04] = useState(null);

  const [readOnly, setReadOnly] = useState(false);

  // 计算结果
  const [result, setResult] = useState(calculateResInit);

  function onChange(e) {
    const target = e.target;
    const name = target.name;
    const value = target.value;

    if (name === 'weightMax') {
      setWeightMax(value);
    }
    else if (name === 'space01') {
      setSpace01(value);
    }
    else if (name === 'space02') {
      setSpace02(value);
    }
    else if (name === 'space03') {
      setSpace03(value);
    }
    else if (name === 'space04') {
      setSpace04(value);
    }
  }

  function calculate() {
    const g = 9.81;
    result.mainMaxLoad = weightMax * g * (space01 - space03) / (2 * space01) * 0.001;
    result.preMaxLoad = weightMax * g * (space01 - space02) / space01 * 0.001;
    result.preMinLoad = weightMax * g * space03 / space01 * 0.001;
    result.preMaxBrakeLoad = result.preMaxLoad + 3.05 * space04 * weightMax * g / (9.81 * space01) * 0.001;

    setResult({ ...result });

    const obj = [
      {
        id: '标识',
        name: '名称',
        code: '代号',
        value: '值'
      },
      {
        id: 189,
        name: '主起落架最大停机载荷',
        code: 'ZZDZH',
        value: result.mainMaxLoad
      },
      {
        id: 190,
        name: '前起落架最大停机载荷',
        code: 'QZDZH',
        value:result.preMaxLoad
      },
      {
        id: 191,
        name: '前起落架最小停机载荷',
        code: 'QZXZH',
        value:result.preMinLoad
      },
      {
        id: 192,
        name: '最大刹车的前轮载荷',
        code: 'QLZH',
        value:result.preMaxBrakeLoad
      }
    ];

    window.electronAPI.createOutputFile(obj, SHUTDOWN_LOAD_CALCULATION_FRONT_THREE);
  }

  useEffect(() => {
    /* command argv */
    window.electronAPI.commandArgv((_event, content) => {
      const objs = JSON.parse(content);
      if (objs.length > 0) {
        setReadOnly(true);
        objs.forEach(obj => {
          if (obj.code === 'QLJ_W') {
            // 设计最大着陆重量
            obj.value !== undefined ? setWeightMax(obj.value) : setWeightMax(20300);
          }
          else if (obj.code === 'QLJ_B') {
            // 飞行器中线与主轮距
            obj.value !== undefined ? setSpace01(obj.value) : setSpace01(90);
          }
          else if (obj.code === 'QLJ_L') {
            // 前起落架与前重心距离
            obj.value !== undefined ? setSpace02(obj.value) : setSpace02(70);
          }
          else if (obj.code === 'QLJ_M') {
            obj.value !== undefined ? setSpace03(obj.value) : setSpace03(20);
          }
          else if (obj.code === 'QLJ_J') {
            obj.value !== undefined ? setSpace04(obj.value) : setSpace04(50);
          }
        });
      }
      else {
        setReadOnly(false);
        setWeightMax(20300);
          setSpace01(100);
        setSpace02(70);
        setSpace03(10);
        setSpace04(50);
      }
    });
  }, []);


  return <div className={"shutdown-load-calculation-front-three"}>
    <div className={"sub-title"}>输入参数</div>
    <Descriptions  bordered column={4}>
      <Descriptions.Item label="飞机的最大总重(kg)" span={2}>
        <Input value={weightMax} name={"weightMax"} onChange={onChange} readOnly={readOnly} />
      </Descriptions.Item>
      <Descriptions.Item label="前起落架与主起落架距离(m)" span={2}>
        <Input value={space01} name={"space01"} onChange={onChange} readOnly={readOnly} />
      </Descriptions.Item>
      <Descriptions.Item label="前起落架与前重心距离(m)" span={2}>
        <Input value={space02} name={"space02"} onChange={onChange} readOnly={readOnly} />
      </Descriptions.Item>
      <Descriptions.Item label="后重心与主起落架距离(m)" span={2}>
        <Input value={space03} name={"space03"} onChange={onChange} readOnly={readOnly} />
      </Descriptions.Item>
      <Descriptions.Item label="前后重心与地面距离(m)" span={2}>
        <Input value={space04} name={"space04"} onChange={onChange} readOnly={readOnly} />
      </Descriptions.Item>
    </Descriptions>

    <div className={"sub-title"}>
      计算结果
      <Button
        type={"text"}
        onClick={calculate}
        style={{ color: "#1677FF" }}
      >
        计算
      </Button>
    </div>
    <Descriptions bordered column={4}>
      <Descriptions.Item label="主起落架最大停机载荷(kN)" span={2}>
        {result.mainMaxLoad}
      </Descriptions.Item>
      <Descriptions.Item label="前起落架最大停机载荷(kN)" span={2}>
        {result.preMaxLoad}
      </Descriptions.Item>
      <Descriptions.Item label="前起落架最小停机载荷(kN)" span={2}>
        {result.preMinLoad}
      </Descriptions.Item>
      <Descriptions.Item label="前起落架最大刹车的前轮载荷(kN)" span={2}>
        {result.preMaxBrakeLoad}
      </Descriptions.Item>
    </Descriptions>
  </div>;
}

export default ShutdownLoadCalculationFrontThree;
